package spe.ui.editor;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.gef.ui.actions.ActionRegistry;
import org.eclipse.gef.ui.actions.DeleteAction;
import org.eclipse.gef.ui.actions.PrintAction;
import org.eclipse.gef.ui.actions.RedoAction;
import org.eclipse.gef.ui.actions.SaveAction;
import org.eclipse.gef.ui.actions.StackAction;
import org.eclipse.gef.ui.actions.UndoAction;
import org.eclipse.gef.ui.actions.UpdateAction;
import org.eclipse.gef.ui.actions.WorkbenchPartAction;
import org.eclipse.jface.action.IAction;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPart;

import spe.ui.action.CancelableDeleteAction;
import spe.ui.action.PropertiesSelectionAction;


public class SPEActionRegistry extends ActionRegistry 
{
	private IEditorPart editorPart;
	
	private List<String> editPartActionIds = new ArrayList<String>();
	private List<String> stackActionIds = new ArrayList<String>();
	private List<String> editorActionIds = new ArrayList<String>();
	
	public SPEActionRegistry(IEditorPart part)
	{
		this.editorPart = part;
		initActions();
	}
	
	private void initActions()
	{
		//addStackAction(new UndoAction(editorPart));
		//addStackAction(new RedoAction(editorPart));
		
		//addEditPartAction(new CancelableDeleteAction((IWorkbenchPart)editorPart));
		addEditPartAction(new DeleteAction((IWorkbenchPart)editorPart));
		addEditPartAction(new PropertiesSelectionAction((IWorkbenchPart)editorPart));
		addEditPartAction(new SaveAction(editorPart));
		registerAction(new PrintAction(editorPart));
	}
	
	private void addStackAction(StackAction action)
	{
		registerAction(action);
		stackActionIds.add(action.getId());
	}
	
	private void addEditPartAction(WorkbenchPartAction action)
	{
		registerAction(action);
		editPartActionIds.add(action.getId());
	}
	
	private void updateActions(List<String> actionIds)
	{
		Iterator<String> ids = actionIds.iterator();
		
		while(ids.hasNext())
		{
			IAction action = getAction(ids.next());
			if ( (action != null) && (action instanceof UpdateAction) )
				((UpdateAction)action).update();
		}
	}
	
	public void updateStackActions()
	{
		updateActions(stackActionIds);
	}
	
	public void updateEditPartActions()
	{
		updateActions(editPartActionIds);
	}
	
	public void updateEditorActions()
	{
		updateActions(editorActionIds);
	}
}
